package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CourseDao;
import com.itheima.dao.store.QuestionDao;
import com.itheima.domain.store.Course;
import com.itheima.domain.store.Question;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.QuestionService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: Web综合案例
 * @author: DingYaDong
 * @create: 2021-06-24 16:48
 */
public class QuestionServiceImpl implements QuestionService {

    @Override
    public String save(Question question,boolean flag) {
        int save = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
            String uuid = UUID.randomUUID().toString();
            question.setId(uuid);
            //设置新创建的题目默认的审核状态为未审核(0)
            question.setReviewStatus("0");
            question.setCreateTime(new Date());
            if (flag){
                //设置当前存储的图片名称为id值
                question.setPicture(uuid);
            }
            //调用dao层
            save = questionDao.save(question);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return uuid;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int delete(Question question) {
        int del = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
            del = questionDao.delete(question);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return del;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int update(Question question,boolean flag) {
        int up = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
            //设置修改的题目默认的审核状态为未审核(0)
            question.setReviewStatus("0");
            if (flag){
                question.setPicture(question.getId());
            }
            up = questionDao.update(question);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return up;
        } catch (Exception e) {

            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Question findById(String id) {
        Question byId = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
            byId = questionDao.findById(id);
            return byId;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Question> findAll() {
        List<Question> questions;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
            questions = questionDao.findAll();
            return questions;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {
        List<Question> questions;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
            //PageHelper设置分页必须出现在查询的前面
            PageHelper.startPage(page,size);
            //查询
            questions = questionDao.findAll();
            PageInfo pageInfo = new PageInfo(questions);
            return pageInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public ByteArrayOutputStream getReport() {

        SqlSession sqlSession = null;
        List<Question> questions = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
            //查询
            questions = questionDao.findAll();


        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        XSSFWorkbook sheets = new XSSFWorkbook();
        XSSFSheet sheet = sheets.createSheet("测试文件");
        XSSFRow row = sheet.createRow(1);
        XSSFCell cell = row.createCell(1);
        cell.setCellValue("在线试题导出信息");
        //设置标题样式
        CellStyle cs_field = sheets.createCellStyle();
        cs_field.setAlignment(HorizontalAlignment.CENTER);
        cell.setCellStyle(cs_field);

        //制作属性头
        String[] title = {"题目ID", "所属公司", "所属目录", "题目简介", "题干描述", "题干配图", "题干分析",
                "题干类型", "题干难度", "是否经典题", "题目状态", "审核状态"};
        //row_2还在内存当中,并没有写入,如果放在for循环里,则会被覆盖掉,故放在for外面
        Row row_2 = sheet.createRow(2);
        for (int i = 0; i < title.length; i++) {
            Cell useCell = row_2.createCell(i + 1);
            useCell.setCellValue(title[i]);
            CellStyle cs = sheets.createCellStyle();
            cs.setAlignment(HorizontalAlignment.CENTER);
            useCell.setCellStyle(cs);
        }



        sheet.addMergedRegion(new CellRangeAddress(1,1,1,11));
        for (int i = 0; i < questions.size(); i++) {
            XSSFRow userRow = sheet.createRow(i+3);
            XSSFCell cell1 = userRow.createCell(1);
            cell1.setCellValue(questions.get(i).getId());

            XSSFCell cell2 = userRow.createCell(2);
            if (!(questions.get(i).getCompany()==null)){
                cell2.setCellValue(questions.get(i).getCompany().getName());
            }



            XSSFCell cell3 = userRow.createCell(3);
            if (!(questions.get(i).getCatalog()==null)) {
                cell3.setCellValue(questions.get(i).getCatalog().getName());
            }
            XSSFCell cell4 = userRow.createCell(4);
            cell4.setCellValue(questions.get(i).getRemark());

            XSSFCell cell5 = userRow.createCell(5);
            cell5.setCellValue(questions.get(i).getSubject());

            XSSFCell cell6 = userRow.createCell(6);
            cell6.setCellValue(questions.get(i).getPicture());
            System.out.println(questions.get(i).getPicture());


            XSSFCell cell7 = userRow.createCell(7);
            cell7.setCellValue(questions.get(i).getAnalysis());


            XSSFCell cell8 = userRow.createCell(8);
            cell8.setCellValue(questions.get(i).getType());

            XSSFCell cell9 = userRow.createCell(9);
            cell9.setCellValue(questions.get(i).getDifficulty());


            XSSFCell cell10 = userRow.createCell(10);
            cell10.setCellValue(questions.get(i).getIsClassic());


            XSSFCell cell11 = userRow.createCell(11);
            cell11.setCellValue(questions.get(i).getState());

            XSSFCell cell12 = userRow.createCell(12);
            cell12.setCellValue(questions.get(i).getReviewStatus());

        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            sheets.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            sheets.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return os;


    }

}
